-
Notifications
You must be signed in to change notification settings - Fork 417
Use Ruby's Etc.nprocessors for processor_count if available #814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
MRI has had Etc.nprocessors since Ruby 2.2, which uses: - sched_getaffinity(): Linux - sysconf(_SC_NPROCESSORS_ONLN): GNU/Linux, NetBSD, FreeBSD, OpenBSD, DragonFly BSD, OpenIndiana, Mac OS X, AIX - GetSystemInfo(): Win32 This should be faster than the alternatives of reading from /proc/cpuinfo, using WIN32OLE, or spawning a new process. This also has the advantage of reporting processor count based on CPU affinity which /usr/bin/nproc did, but reading /proc/cpuinfo does not. When using this value for controlling the level of parallelism, we should ideally be using the processes CPU affinity.
|
$ ruby -r rubygems -r etc -e 'puts Etc.nprocessors' (4 is the correct answer for this machine) $ ruby -v $ uname -a |
| def compute_processor_count | ||
| if Concurrent.on_jruby? | ||
| java.lang.Runtime.getRuntime.availableProcessors | ||
| elsif defined?(Etc) && Etc.respond_to?(:nprocessors) && (nprocessor = Etc.nprocessors rescue nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Etc is not included by default, so should this probe also include a require "etc"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Good catch. I added it as a top-level require (I believe all supported rubies should support etc, older ones just don't implement nprocessors).
eregon
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great to me.
pitr-ch
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
In 0371ed4 from the discussions in #576, we switched the default method of detecting processor count from
/usr/bin/nprocto reading/proc/cpuinfo. This avoids a fork, but means that we no longer respect CPU affinity, which we should want if using this value to control parallelism. I imagine it should work on Alpha too.MRI has had
Etc.nprocessorssince Ruby 2.2, which uses:This should be faster than the alternatives of reading from
/proc/cpuinfo, using WIN32OLE, or spawning a new process (though all should be plenty fast since this is memoized). It also will take into account CPU affinity.Before:
After:
cc @pitr-ch @matthewd @graaff